package cn.zsb.anoprinter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author oldliu
 * @since 1.0
 */
@Service(value = "a4")
public class TextPaper {
    @Value("15")//默认是A4 15*20
    private int words;//一行打几个字
    @Value("20")
    private int lines;//一张纸能打几行

    public void print(String str){
        char[] cr = str.toCharArray();
        //w是当前行打了几个字，l是当前页打了几行，p是当前是第几页
        int w=0,l=0,p=0;
        for (char c:cr){
            System.out.print(c);
            w++;
            if(w>=words){
                l++;
                w=0;
                System.out.println();
                if(l>=lines){
                    p++;
                    l=0;
                    System.out.println("--"+p+"--");//页码
                }
            }
        }
        if(w>0||l>0){
            System.out.println("\n--"+(++p)+"--");//页码
        }
    }

    public void setWords(int words) {
        this.words = words;
    }

    public void setLines(int lines) {
        this.lines = lines;
    }

    @Override
    public String toString() {
        return "TextPaper{" +
                "words=" + words +
                ", lines=" + lines +
                '}';
    }
}
